package com.wteam.dragon.shop.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.shop.mapper.BrandHomeMapper;
import com.wteam.dragon.shop.mapper.BrandMapper;
import com.wteam.dragon.shop.pojo.bean.Brand;
import com.wteam.dragon.shop.pojo.bean.BrandHome;
import com.wteam.dragon.shop.service.BrandHomeService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 陈晓辉
 * @Date 2021/3/25 16:57
 */
@Service
@RequiredArgsConstructor
public class BrandHomeServiceImpl implements BrandHomeService {
    private final BrandHomeMapper brandHomeMapper;
    private final BrandMapper brandMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addBrandHome(List<Long> ids) {
        ids.forEach(id -> {
            Brand brand = brandMapper.selectById(id);
            BrandHome brandHome = new BrandHome();
            brandHome.setBrandId(brand.getId());
            brandHome.setBrandName(brand.getName());
            brandHome.setBrandNameEn(brand.getNameEn());
            brandHomeMapper.insert(brandHome);
        });
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delBrandHome(List<Long> ids) {
        brandHomeMapper.deleteBatchIds(ids);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editBrandHomeSort(Long id, Long sort) {
        BrandHome brandHome = new BrandHome();
        brandHome.setId(id);
        brandHome.setSort(sort);
        brandHomeMapper.updateById(brandHome);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更新成功");
        return resultMessage;
    }


    @Override
    public ResultMessage pageBrandHome(Page<BrandHome> page, String brandName, Integer recommendStatus) {
        QueryWrapper<BrandHome> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(brandName != null, BrandHome::getBrandName, brandName)
                .eq(recommendStatus != null, BrandHome::getRecommendStatus, recommendStatus)
                .orderByAsc(BrandHome::getSort);
        Page<BrandHome> brandHomePage = brandHomeMapper.selectPage(page, queryWrapper);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("brandHomePage", brandHomePage);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editBrandHomeStatus(List<Long> ids, Integer status) {
        ids.forEach(id -> {
            BrandHome brandHome = new BrandHome();
            brandHome.setId(id);
            brandHome.setRecommendStatus(status);
            brandHomeMapper.updateById(brandHome);
        });
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更改成功");
        return resultMessage;
    }
}
